﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BigPang.Common.Service;
using System.Windows.Forms;
using BigPang.Common.GamePlay;
using BigPang.Game.Objects;
using BigPang.Common.GamePlay.Objects;
using System.Reflection;

namespace BigPang.Game
{
    public class GamePlay : IGameCallback
    {
        static GamePlay()
        {
            ProxyManager.RegisterProxy(typeof(CTank), typeof(Tank));
        }

        #region IGameCallback 成员

        public void OnPlayerStartMove(int id, int x, int y, int dir)
        {
            if (id == Program.mObjId)
                return;

            CTank tank = GameManager.Get(id) as CTank;

            tank.mSprite.SetPosition(x, y);
            tank.mSprite.StartMove((Render.Sprite.Direction)dir, 100);
        }

        public void OnPlayerStopMove(int id, int x, int y)
        {
            if (id == Program.mObjId)
                return;

            CTank tank = GameManager.Get(id) as CTank;
            tank.mSprite.StopMove();
            tank.mSprite.SetPosition(x, y);
        }

        public void OnObjectCreated(string type, string name, int id, int x, int y)
        {
            Assembly asm = Assembly.GetAssembly( typeof(GameObject) );

            string ctype = ProxyManager.GetProxyType(asm.GetType(type)).ToString();
            GameObject obj = Assembly.GetExecutingAssembly().CreateInstance(ctype) as GameObject;
            obj.Name = name;
            obj.Id = id;
            obj.Register();

            obj.SetPosition(x, y);

        }

        public void OnObjectDestory(int id)
        {
            throw new NotImplementedException();
        }

        public void OnPlayerJoin()
        {
            throw new NotImplementedException();
        }

        public void OnPlayerQuit()
        {
            throw new NotImplementedException();
        }

        public void OnAction(int id, int actionId)
        {
            CTank tank = GameManager.Get(id) as CTank;
            tank.OnAction((CTank.Action)actionId);
        }

        #endregion

        #region IGameCallback 成员


        public void OnHit(int id, int sourceId)
        {
            CTank tank = GameManager.Get(id) as CTank;
            tank.OnHit(GameManager.Get(sourceId) as CTank);
        }

        #endregion

        #region IGameCallback 成员


        public void TakeControl(int id)
        {
            CTank tank = GameManager.Get(id) as CTank;

            if (tank != null)
                tank.TakeControl();
            else
                Program.mObjId = id;
            
        }

        #endregion
    }
}
